package com.hx.company.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;

import com.hx.company.dao.MerDao;
import com.hx.company.vo.Food;
import com.hx.company.vo.Merchant;
import com.hx.company.vo.MerchantLogin;
import com.hx.company.vo.PicName;
import com.hx.company.vo.UserLogin;
@Repository("mrd")
public class MerDaoImpl implements MerDao {
	@PersistenceContext(name="un")
	private EntityManager em;
	
	
	@Override
	public List<MerchantLogin> find(MerchantLogin m) {
		String s="select c from MerchantLogin c where c.mphone=:mphone";
		List<MerchantLogin> cs=em.createQuery(s)
				.setParameter("mphone", m.getMphone())
				.getResultList();
		
		return cs;
	}


	@Override
	public Boolean addMer(Merchant m, MerchantLogin ml) {
		String s="select c from Merchant c where c.mphone=:mphone ";
		List<MerchantLogin> cs=em.createQuery(s)
				.setParameter("mphone", m.getMphone())
				.getResultList();
		Boolean q=false;
		if(cs.size()==0){
			em.persist(m);
			em.persist(ml);
			q=true;
		}
		System.out.println(q+"++");
		return q;
	}


	@Override
	public String getPicName() {
		String s="SELECT c from  Merchant c order by mid desc";
		List<Merchant> ls=em.createQuery(s).getResultList();
		
		Merchant c=ls.get(0);
		System.out.println(c.getMname()+"---");
		return c.getMname();
	}


	

	@Override
	public String getMid(String phone) {
		String s="select c from Merchant c where c.mphone=:mphone ";
		List<Merchant> cs=em.createQuery(s)
				.setParameter("mphone",phone )
				.getResultList();
		String ss="";
		for(Merchant c:cs){
			ss=c.getMid()+"";
		}
		return ss;
	}


	@Override
	public List<Food> getFood(String mid) {
		String s="select c from Food c where c.mid=:mid ";
		List<Food> cs=em.createQuery(s)
				.setParameter("mid",Integer.parseInt(mid) )
				.getResultList();
		
		
		return cs;
	}
	
	

}
